(function(d) {
    var c = mobiscroll,
        a = c.$,
        e = c.presets.scroller,
        b = c.util.datetime,
        f = c.util,
        g = f.testTouch,
        h = {
            autoCorrect: true,
            showSelector: true,
            minRange: 1,
            rangeTap: true,
            fromText: 'Start',
            toText: 'End'
        };
    c.presetShort('range');
    e.range = function(f) {
        function H(a, b) {
            if (a) {
                a.setFullYear(b.getFullYear());
                a.setMonth(b.getMonth());
                a.setDate(b.getDate());
            }
        }

        function N(b) {
            f._startDate = o = k;
            f._endDate = p = j;
            if (c.startInput) {
                a(c.startInput).val(r);
                if (b) {
                    a(c.startInput).trigger('change');
                }
            }
            if (c.endInput) {
                a(c.endInput).val(q);
                if (b) {
                    a(c.endInput).trigger('change');
                }
            }
        }

        function C(b, d) {
            var a = true;
            if (b && k && j) {
                if (j - k > c.maxRange - 1) {
                    if (i) {
                        k = new Date(j - c.maxRange + 1);
                    } else {
                        j = new Date(+k + c.maxRange - 1);
                    }
                }
                if (j - k < c.minRange - 1) {
                    if (i) {
                        k = new Date(j - c.minRange + 1);
                    } else {
                        j = new Date(+k + c.minRange - 1);
                    }
                }
            }
            if (!k || !j) {
                a = false;
            }
            if (d) {
                M();
            }
            return a;
        }

        function O() {
            return k && j ? Math.max(1, Math.round((new Date(j).setHours(0, 0, 0, 0) - new Date(k).setHours(0, 0, 0, 0)) / 86400000) + 1) : 0;
        }

        function L(a) {
            a.addClass('mbsc-range-btn-sel').attr('aria-checked', 'true').find('.mbsc-range-btn').addClass(x);
        }

        function E() {
            if (D && l) {
                a('.mbsc-range-btn-c', l).removeClass('mbsc-range-btn-sel').removeAttr('aria-checked').find('.mbsc-range-btn', l).removeClass(x);
                L(a('.mbsc-range-btn-c', l).eq(i));
            }
        }

        function M() {
            var d, h, e, o, f, g = 0,
                p = m || !i ? ' mbsc-cal-day-hl mbsc-cal-sel-start' : ' mbsc-cal-sel-start',
                s = m || i ? ' mbsc-cal-day-hl mbsc-cal-sel-end' : ' mbsc-cal-sel-end';
            r = k ? b.formatDate(n, k, c) : '';
            q = j ? b.formatDate(n, j, c) : '';
            if (l) {
                a('.mbsc-range-btn-v-start', l).html(r || '&nbsp;');
                a('.mbsc-range-btn-v-end', l).html(q || '&nbsp;');
                d = k ? new Date(k) : null;
                e = j ? new Date(j) : null;
                if (!d && e) {
                    d = new Date(e);
                }
                if (!e && d) {
                    e = new Date(d);
                }
                f = i ? e : d;
                a('.mbsc-cal-table .mbsc-cal-day-sel .mbsc-cal-day-i', l).removeClass(x);
                a('.mbsc-cal-table .mbsc-cal-day-hl', l).removeClass(z);
                a('.mbsc-cal-table .mbsc-cal-day-sel', l).removeClass('mbsc-cal-day-sel mbsc-cal-sel-start mbsc-cal-sel-end').removeAttr('aria-selected');
                if (d && e) {
                    h = d.setHours(0, 0, 0, 0);
                    o = e.setHours(0, 0, 0, 0);
                    while (e >= d && g < 84) {
                        a('.mbsc-cal-day[data-full="' + f.getFullYear() + '-' + f.getMonth() + '-' + f.getDate() + '"]', l).addClass('mbsc-cal-day-sel' + (f.getTime() === h ? p : '') + (f.getTime() === o ? s : '')).attr('aria-selected', 'true').find('.mbsc-cal-day-i ').addClass(x);
                        f.setDate(f.getDate() + (i ? -1 : 1));
                        g++;
                    }
                }
            }
        }
        var w, l, u, n, A, t, v, r, q, k, j, I, J, G, D, o = f._startDate,
            p = f._endDate,
            i = 0,
            y = new Date(),
            K = a.extend({}, f.settings),
            c = a.extend(f.settings, h, K),
            B = c.anchor,
            m = c.rangeTap,
            x = c.activeClass || '',
            F = 'mbsc-fr-btn-d ' + (c.disabledClass || ''),
            z = 'mbsc-cal-day-hl',
            s = c.defaultValue === null ? [] : c.defaultValue || [new Date(y.setHours(0, 0, 0, 0)), new Date(y.getFullYear(), y.getMonth(), y.getDate() + 6, 23, 59, 59, 999)];
	    if (m) {
            c.tabs = true;
        }
        A = e.calbase.call(this, f);
        w = a.extend({}, A);
        n = f.format;
        I = c.controls.join('') === 'time';
        D = c.controls.length == 1 && c.controls[0] == 'calendar' ? c.showSelector : true;
        if (c.startInput) {
            J = a(c.startInput).prop('readonly');
            f.attachShow(a(c.startInput).prop('readonly', true), function() {
                i = 0;
                c.anchor = B || a(c.startInput);
            });
        }
        if (c.endInput) {
            G = a(c.endInput).prop('readonly');
            f.attachShow(a(c.endInput).prop('readonly', true), function() {
                i = 1;
                c.anchor = B || a(c.endInput);
            });
        }
        f.setVal = function(g, l, m, h, s) {
            var a = g || [],
                e = g;
            if (a[0] === d || a[0] === null || a[0].getTime) {
                v = true;
                k = a[0] || null;
                r = k ? b.formatDate(n, k, c) : '';
                if (!i) {
                    e = w.parseValue(r, f);
                }
            }
            if (a[1] === d || a[1] === null || a[1].getTime) {
                v = true;
                j = a[1] || null;
                q = j ? b.formatDate(n, j, c) : '';
                if (i) {
                    e = w.parseValue(q, f);
                }
            }
            if (!h) {
                f._startDate = o = k;
                f._endDate = p = j;
            }
            f._setVal(e, l, m, h, s);
        };
        f.getVal = function(a) {
            return a ? [k, j] : f._hasValue ? [o, p] : null;
        };
        f.getDayProps = function(b) {
            var a = k ? new Date(k.getFullYear(), k.getMonth(), k.getDate()) : null,
                c = j ? new Date(j.getFullYear(), j.getMonth(), j.getDate()) : null;
            return {
                selected: a && c && b >= a && b <= j,
                cssClass: ((m || !i) && a && a.getTime() === b.getTime() || (m || i) && c && c.getTime() === b.getTime() ? z : '') + (a && a.getTime() === b.getTime() ? ' mbsc-cal-sel-start' : '') + (c && c.getTime() === b.getTime() ? ' mbsc-cal-sel-end' : '')
            };
        };
        f.setActiveDate = function(c) {
            var b;
            i = c == 'start' ? 0 : 1;
            b = c == 'start' ? k : j;
            if (f.isVisible()) {
                E();
                if (!m) {
                    a('.mbsc-cal-table .mbsc-cal-day-hl', l).removeClass(z);
                    if (b) {
                        a('.mbsc-cal-day[data-full="' + b.getFullYear() + '-' + b.getMonth() + '-' + b.getDate() + '"]', l).addClass(z);
                    }
                }
                if (b) {
                    t = true;
                    f.setDate(b, false, 1000, true);
                }
            }
        };
        f.getValue = f.getVal;
        a.extend(A, {
            highlight: false,
            outerMonthChange: false,
            formatValue: function() {
                return r + (c.endInput ? '' : q ? ' - ' + q : '');
            },
            parseValue: function(e) {
                var d = e ? e.split(' - ') : [];
                c.defaultValue = s[1];
                if (c.endInput) {
                    p = a(c.endInput).val() ? b.parseDate(n, a(c.endInput).val(), c) : s[1];
                } else {
                    p = d[1] ? b.parseDate(n, d[1], c) : s[1];
                }
                c.defaultValue = s[0];
                if (c.startInput) {
                    o = a(c.startInput).val() ? b.parseDate(n, a(c.startInput).val(), c) : s[0];
                } else {
                    o = d[0] ? b.parseDate(n, d[0], c) : s[0];
                }
                c.defaultValue = s[i];
                r = o ? b.formatDate(n, o, c) : '';
                q = p ? b.formatDate(n, p, c) : '';
                f._startDate = o;
                f._endDate = p;
                return w.parseValue(i ? q : r, f);
            },
            onFill: function(a) {
                N(a.change);
            },
            onBeforeClose: function(a) {
                if (a.button === 'set' && !C(true, true)) {
                    f.setActiveDate(i ? 'start' : 'end');
                    return false;
                }
            },
            onHide: function() {
                w.onHide.call(f);
                i = 0;
                l = null;
                c.anchor = B;
            },
            onClear: function() {
                if (m) {
                    i = 0;
                }
            },
            onBeforeShow: function() {
                c.headerText = false;
                k = o;
                j = p;
                if (c.counter) {
                    c.headerText = function() {
                        var a = O();
                        return (a > 1 ? c.selectedPluralText || c.selectedText : c.selectedText).replace(/{count}/, a);
                    };
                }
                v = true;
            },
            onMarkupReady: function(b) {
                var d;
                l = a(b.target);
                if (k) {
                    t = true;
                    f.setDate(k, false, 0, true);
                    k = f.getDate(true);
                }
                if (j) {
                    t = true;
                    f.setDate(j, false, 0, true);
                    j = f.getDate(true);
                }
                if (i && j || !i && k) {
                    t = true;
                    f.setDate(i ? j : k, false, 0, true);
                }
                w.onMarkupReady.call(this, b);
                l.addClass('mbsc-range');
                if (D) {
                    d = '<div class="mbsc-range-btn-t" role="radiogroup">' + '<div class="mbsc-range-btn-c mbsc-range-btn-start"><div role="radio" class="mbsc-fr-btn-e mbsc-fr-btn-nhl mbsc-range-btn">' + c.fromText + '<div class="mbsc-range-btn-v mbsc-range-btn-v-start">' + (r || '&nbsp;') + '</div></div></div>' + '<div class="mbsc-range-btn-c mbsc-range-btn-end"><div role="radio" class="mbsc-fr-btn-e mbsc-fr-btn-nhl mbsc-range-btn">' + c.toText + '<div class="mbsc-range-btn-v mbsc-range-btn-v-end">' + (q || '&nbsp;') + '</div></div></div>' + '</div>';
                    a('.mbsc-cal-tabs', l).before(d);
                    E();
                }
                a('.mbsc-range-btn-c', l).on('touchstart click', function(b) {
                    if (g(b, this)) {
                        f.showMonthView();
                        f.setActiveDate(a(this).index() ? 'end' : 'start');
                    }
                });
            },
            onDayChange: function(a) {
                a.active = i ? 'end' : 'start';
                u = true;
            },
            onSetDate: function(g) {
                var b = g.date,
                    e = f.order;
                if (!t) {
                    if (e.h === d) {
                        b.setHours(i ? 23 : 0);
                    }
                    if (e.i === d) {
                        b.setMinutes(i ? 59 : 0);
                    }
                    if (e.s === d) {
                        b.setSeconds(i ? 59 : 0);
                    }
                    b.setMilliseconds(i ? 999 : 0);
                    if (!v || u) {
                        if (m && u) {
                            if (i == 1 && b < k) {
                                i = 0;
                            }
                            if (i) {
                                b.setHours(23, 59, 59, 999);
                            } else {
                                b.setHours(0, 0, 0, 0);
                            }
                        }
                        if (i) {
                            j = new Date(b);
                        } else {
                            k = new Date(b);
                        }
                        if (I) {
                            H(k, b);
                            H(j, b);
                        }
                        if (m && u && !i) {
                            j = null;
                        }
                    }
                }
                f._isValid = C(v || u || c.autoCorrect, !t);
                g.active = i ? 'end' : 'start';
                if (!t && m) {
                    if (u) {
                        i = i ? 0 : 1;
                    }
                    E();
                }
                if (f.isVisible()) {
                    if (f._isValid) {
                        a('.mbsc-fr-btn-s .mbsc-fr-btn', f._markup).removeClass(F);
                    } else {
                        a('.mbsc-fr-btn-s .mbsc-fr-btn', f._markup).addClass(F);
                    }
                }
                u = false;
                v = false;
                t = false;
            },
            onTabChange: function(a) {
                if (a.tab != 'calendar') {
                    f.setDate(i ? j : k, false, 1000, true);
                }
                C(true, true);
            },
            onDestroy: function() {
                a(c.startInput).prop('readonly', J);
                a(c.endInput).prop('readonly', G);
            }
        });
        return A;
    };
}());